package com.gph.saviorframework.charts.controller;

import com.alibaba.dubbo.common.json.Jackson;
import com.gph.saviorframework.Constants;
import com.gph.saviorframework.echarts.Option;
import com.gph.saviorframework.echarts.axis.CategoryAxis;
import com.gph.saviorframework.echarts.axis.ValueAxis;
import com.gph.saviorframework.echarts.code.*;
import com.gph.saviorframework.echarts.data.PointData;
import com.gph.saviorframework.echarts.feature.MagicType;
import com.gph.saviorframework.echarts.json.GsonUtil;
import com.gph.saviorframework.echarts.series.Bar;
import com.gph.saviorframework.echarts.style.ItemStyle;
import com.gph.saviorframework.echarts.style.LineStyle;
import com.gph.saviorframework.echarts.style.itemstyle.Normal;
import org.activiti.engine.repository.Model;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * echarts图表demo
 */
@Controller
@RequestMapping("/" + Constants.FRAMEWORK_REQUEST_PATH + "/charts")
public class EChartsDemoController {

    private final String viewPrefix = Constants.FRAMEWORK_REQUEST_PATH + "/charts/bar/";

    @RequestMapping(value = "/bar/std",method = RequestMethod.GET)
    public String stdBarPage(){
        return viewPrefix+"bar_std";
    }

    @RequestMapping(value = "/bar/std",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap genStdBarChart(){

        ModelMap modelMap = new ModelMap();

        Option option = new Option();
        option.title().text("某地区蒸发量和降水量");
        option.tooltip().trigger(Trigger.axis);
        option.legend("蒸发量", "降水量");
        option.toolbox().show(true).feature(Tool.mark, Tool.dataView, new MagicType(Magic.line, Magic.bar).show(true),
                Tool.restore, Tool.saveAsImage);
        option.calculable(true);
        option.xAxis(new CategoryAxis().data("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"));
        option.yAxis(new ValueAxis());

        Bar bar = new Bar("蒸发量");
        bar.data(2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3);
        bar.markPoint().data(new PointData().type(MarkType.max).name("最大值"), new PointData().type(MarkType.min).name("最小值"));
        bar.markLine().data(new PointData().type(MarkType.average).name("平均值"));

        Bar bar2 = new Bar("降水量");
        List<Double> list = Arrays.asList(2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3);
        bar2.setData(list);
        bar2.markPoint().data(new PointData("年最高", 182.2).xAxis(7).yAxis(183).symbolSize(18), new PointData("年最低", 2.3).xAxis(11).yAxis(3));
        bar2.markLine().data(new PointData().type(MarkType.average).name("平均值"));

        option.series(bar, bar2);

        modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY,Boolean.TRUE);
        modelMap.addAttribute("data", GsonUtil.format(option));

        return modelMap;
    }

    @RequestMapping(value = "/bar/dj",method = RequestMethod.GET)
    public String djBarPage(){
        return viewPrefix+"bar_dj";
    }

    @RequestMapping(value = "/bar/dj",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap gendjBarChart(){

        ModelMap modelMap = new ModelMap();

        Option option = new Option();
        option.title().text("堆积柱状图例子");
        option.tooltip().trigger(Trigger.axis);
        option.legend("直接访问","邮件营销","联盟广告","视频广告","搜索引擎","百度","谷歌","必应","其他");
        option.toolbox().show(true).feature(Tool.mark, Tool.dataView,
                new MagicType(Magic.line, Magic.bar,Magic.stack,Magic.tiled).show(true),
                Tool.restore,
                Tool.saveAsImage).orient(Orient.vertical).x(X.right).y(Y.center);

        option.calculable(true);

        option.xAxis(new CategoryAxis().data("周一","周二","周三","周四","周五","周六","周日"));
        option.yAxis(new ValueAxis().type(AxisType.value));

        Bar bar = new Bar("直接访问");
        bar.data(320, 332, 301, 334, 390, 330, 320);

        Bar bar2 = new Bar("邮件营销");
        bar2.data(120, 132, 101, 134, 90, 230, 210);

        Bar bar3 = new Bar("联盟广告");
        bar3.data(220, 182, 191, 234, 290, 330, 310);


        Bar bar4 = new Bar("视频广告");
        bar4.data(150, 232, 201, 154, 190, 330, 410);

        Bar bar5 = new Bar("搜索引擎");
        bar5.data(862, 1018, 964, 1026, 1679, 1600, 1570);
        bar5.markLine().itemStyle().normal().lineStyle().type(LineType.dashed);
        List<PointData> pointDatas = new ArrayList<PointData>();
        pointDatas.add(new PointData().type(MarkType.min).name("最小值"));
        pointDatas.add(new PointData().type(MarkType.max).name("最大值"));
        bar5.markLine().data(pointDatas);

        Bar bar6 = new Bar("百度");
        bar6.setBarWidth(5);
        bar6.data(620, 732, 701, 734, 1090, 1130, 1120);

        Bar bar7 = new Bar("谷歌");
        bar7.data(120, 132, 101, 134, 290, 230, 220);

        Bar bar8 = new Bar("必应");
        bar8.data(60, 72, 71, 74, 190, 130, 110);

        Bar bar9 = new Bar("必应");
        bar9.data(62, 82, 91, 84, 109, 110, 120);

        option.series(bar, bar2, bar3, bar4, bar5,bar6,bar7,bar8,bar9);

        modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY,Boolean.TRUE);
        modelMap.addAttribute("data", GsonUtil.format(option));

        return modelMap;
    }

    @RequestMapping(value = "/bar/wdj",method = RequestMethod.GET)
    public String wdjBarPage(){
        return viewPrefix+"bar_wdj";
    }

    @RequestMapping(value = "/bar/wdj",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap genWdjBarChart(){

        ModelMap modelMap = new ModelMap();

        Option option = new Option();
        option.title().text("温度计式图表");
        option.tooltip().trigger(Trigger.axis).axisPointer().type(PointerType.shadow);
        option.tooltip().formatter("function (params){" +
                "            return params[0].name + '<br/>'" +
                "                   + params[0].seriesName + ' : ' + params[0].value + '<br/>'" +
                "                   + params[1].seriesName + ' : ' + (params[1].value + params[0].value);" +
                "        }");
        option.legend("Acutal", "Forecast");
        option.toolbox().show(true).feature(Tool.mark, Tool.dataView,
                Tool.restore,
                Tool.saveAsImage);

        option.calculable(true);

        option.xAxis(new CategoryAxis().data("Cosco","CMA","APL","OOCL","Wanhai","Zim"));
        option.yAxis(new ValueAxis().boundaryGap(0, 0.1).type(AxisType.value));

        Bar bar = new Bar("Acutal");
        bar.setStack("sum");
        bar.setBarCategoryGap("50%");
        bar.itemStyle().normal().color("tomato").
                barBorderColor("tomato").barBorderWidth(6).barBorderRadius(0)
                .label().position(Position.inside).show(true);
        bar.data(260, 200, 220, 120, 100, 80);

        Bar bar2 = new Bar("Forecast");
        bar2.setStack("sum");
        bar2.itemStyle().normal().color("#fff").
                barBorderColor("tomato").barBorderWidth(6).barBorderRadius(0)
                .label().position(Position.top).show(true).formatter("function (params) {" +
                "                            for (var i = 0, l = option.xAxis[0].data.length; i < l; i++) {" +
                "                                if (option.xAxis[0].data[i] == params.name) {" +
                "                                    return option.series[0].data[i] + params.value;" +
                "                                }" +
                "                            }" +
                "                        }").textStyle().color("tomato");
        bar2.data(40, 80, 50, 80, 80, 70);
        option.series(bar, bar2);

        modelMap.addAttribute(Constants.DEFAULT_SUCCESS_KEY,Boolean.TRUE);
        modelMap.addAttribute("data", GsonUtil.format(option));

        return modelMap;
    }
}
